<?php

namespace App\Admin\Controllers;

use App\Admin\Selectable\BusinessTimes;
use App\Admin\Selectable\Scales;
use App\Models\BusinessTime;
use App\Models\Pattern;
use App\Models\Scale;
use App\Models\SetMeal;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class PatternController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '模式';

    protected $status = [1=>'清唱',2=>'套餐',3=>'最低消费'];

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Pattern());

        $grid->number('序号');
        $grid->rows(function ($row, $number) {
            $row->column('number', $number+1);
        });
        $grid->column('scale.name', __('房间类型'));
        $grid->column('type', __('消费模式'))->using($this->status);
        $grid->column('current_price', __('时价'));
        $grid->column('add_price', __('加时价'));
        $grid->column('minimum_price', __('最低消费金额'));
        $grid->column('minimum_time', __('最低消费时间'));

        $grid->filter(function($filter){
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            // 在这里添加字段过滤器
            $filter->equal('scale.name', '房间类型');
            $filter->equal('type', '消费模式')->select($this->status);
        });


        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Pattern::findOrFail($id));

        $show->field('scale.name', __('房间类型'));
        $show->field('business_time_id', __('时间段'))->as(function ($id){
            $start = BusinessTime::find($id)->start;
            $end = BusinessTime::find($id)->end;
            return date('H:i:s',$start-strtotime('today')).'-'.date('H:i:s',$end-strtotime('today'));
        });;
        $show->field('type', __('消费模式'))->using($this->status);
        $show->field('current_price', __('时价'));
        $show->field('add_price', __('加时价'));
        $show->field('minimum_price', __('最低消费金额'));
        $show->field('minimum_time', __('最低消费时间'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Pattern());

        $form->hidden('store_id', __('门店id'));
//        $form->belongsTo('scale_id', Scales::class, '房型')->required();
        $form->select('scale_id', '房型')->options(
            Scale::where('store_id', Admin::user()->store_id)->get()->pluck('name', 'id')
        )->required();

        $form->belongsTo('business_time_id', BusinessTimes::class, '时间段')->required();

        // 消费模式联动组件
        $form->radioButton('type', '消费模式')
            ->options([1=>'清唱',2=>'套餐',3=>'最低消费'])
            ->when(1, function (Form $form) {
                $form->currency('current_price', __('时价'))
                    ->symbol('￥')
                    ->rules('required|min:0');
            })
            ->when(2, function (Form $form) {

                $form->listbox('setmeals', '套餐')
                    ->options(SetMeal::All()->pluck('name', 'id'))
                    ->rules('required');
            })
            ->when(3, function (Form $form) {

                $form->currency('current_price', __('最低消费时价'))
                    ->symbol('￥')
                    ->rules('required|min:0');
            })->rules('required');

        $form->currency('add_price', __('加时价'))
            ->symbol('￥');

        $form->currency('minimum_price', __('最低消费金额'))
            ->symbol('￥');

        $form->number('minimum_time', __('最低消费时间'));

        $form->saving(function (Form $form) {
            $form->store_id = Admin::user()->store_id;
        });

        return $form;
    }
}
